package com.huxiu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.huxiu.net.GsonRequest;
import com.huxiu.net.NetworkConstants;
import com.huxiu.net.responses.BaseResponse;
import com.huxiu.utils.Constants;
import com.huxiu.utils.ErrorResponseException;
import com.huxiu.utils.UmenEvent;
import com.huxiu.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.umeng.analytics.MobclickAgent;

import java.util.LinkedHashMap;
import java.util.Map;

public class SubmitHuatiActivity extends Activity {
    //Tag
    private static final String TAG = SubmitHuatiActivity.class.getName();
    @ViewInject(R.id.edit_title)
    private EditText edit_title;

    @ViewInject(R.id.edit_content)
    private EditText edit_content;

    @ViewInject(R.id.text_exit)
    private TextView text_exit;

    @ViewInject(R.id.text_sure)
    private TextView text_sure;

    @ViewInject(R.id.back)
    private ImageView back;

    private SubmitHuatiActivity instance;

    private int id;

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submithuati);
        instance = this;
        ViewUtils.inject(this);
        initdata();
    }

    private void initdata() {
        if (getIntent().hasExtra(Constants.GROUP_GROUP_ITEM_ID)) {
            id = getIntent().getIntExtra(Constants.GROUP_GROUP_ITEM_ID, 0);
        }
        edit_title.requestFocus();
    }





    private void submitData() {
        String url = NetworkConstants.THREAD_ADD;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("forum_id", id + "");
        params.put("title", edit_title.getText().toString());
        params.put("content", edit_content.getText().toString());

        GsonRequest<BaseResponse> request = new GsonRequest<>(url, Request.Method.POST,
                BaseResponse.class, false, params, new Response.Listener<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse response) {
                if (response.isSuccess()) {
                    Utils.showToast("发表成功");
                    Intent intent = new Intent(Constants.INTENT_HUATI_SUCCESS);
                    UmenEvent.UmengEventMap(instance, UmenEvent.APP_TOPIC_LIST, UmenEvent.TOPIC_LIST_TOPIC);
                    LocalBroadcastManager.getInstance(APP.getInstance()).sendBroadcast(intent);
                    finish();
                } else {
                    Utils.showToast(response.error.message);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Throwable t = error.getCause();
                if (t instanceof ErrorResponseException) {
                    Utils.showToast(error.getMessage());
                } else {
                    Utils.showToast(R.string.generic_failure);
                };
            }
        });
        BaseActivity.getmQueue().add(request);
        //        mQueue.start();
    }


    @OnClick({R.id.back,R.id.text_sure,R.id.text_exit})
    public void monClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                instance.finish();
                break;
            case R.id.text_sure:
                if (TextUtils.isEmpty(edit_title.getText().toString().trim())) {
                    Utils.showToast(getResources().getString(R.string.qunzu_sub_huati_alert_title));
                    return;
                }
                if (TextUtils.isEmpty(edit_content.getText().toString().trim())) {
                    Utils.showToast(getResources().getString(R.string.qunzu_sub_huati_alert_content));
                    return;
                }
                submitData();
                break;
            case R.id.text_exit:
                finish();
                break;
        }

    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }

}
